package ltd.followkba.comment.utils;

import java.util.ArrayList;
import java.util.List;

import ltd.advskin.base.KcTwoListener;

/**
 * Created by Administrator on 2018/6/22.
 */

public class CMultiThread {
    private boolean isStopTask;
    private KcTwoListener mKcTwoListener;
    private List<Object> mLists;
    private byte gIndex;
    public long g8TimeMillis;

    public void setListener(int index, KcTwoListener kcListener) {
        gIndex = (byte) index;
        mKcTwoListener = kcListener;
    }
    public void setTask(Object object) {
        isStopTask = false;
        if (mLists == null){
            mLists = new ArrayList<>();
            mLists.add(object);
            new Thread(new Runnable() {
                @Override
                public void run () {
//                    MSTRING("CMultiThread setTask B "+isStopTask+" "+mKcTwoListener+" "+mLists);
                    long timeMillis = System.currentTimeMillis();
                    if (mKcTwoListener != null) {
                        while (mLists.size() > 0) {
                            if (!isStopTask) {
                                mKcTwoListener.onMessage(gIndex, mLists.get(0));
                            }
                            mLists.remove(0);
                        }
                    }
                    isStopTask = false;
                    mLists = null;
                    g8TimeMillis += System.currentTimeMillis() - timeMillis;

                }
            }).start();
        }else {
            mLists.add(object);
        }
    }
    public int getBusy(){
        return (mLists != null) ? mLists.size() : 0;
    }
    public void stopTask(){
        isStopTask = true;
    }
    public boolean isTaskStop(){
        return  (mLists != null) ?isStopTask : true;
    }
}
